package util.yahoo;

import java.io.StringReader;

import org.apache.commons.digester.Digester;

import vo.yahoo.ListMessageResponse;
import vo.yahoo.MessageInfo;

public class ListMessagesDigester {

	public static void main(String[] args) {
		String str = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><ListMessagesResponse startMid=\"0\" startInfo=\"0\" numMid=\"3\" numInfo=\"3\"><folder unread=\"0\" total=\"3\" size=\"17457\" isSystem=\"true\"><folderInfo fid=\"Inbox\" name=\"Inbox\"/></folder><mid>1_1182_AGdhxEIAAPMVRU03sAyS0Ho0JD4</mid><mid>1_720_ACRu/NgAAJYpRMcNZQFmVTRFZkA</mid><mid>1_22_ACBu/NgAAPRJRJmdhgLC+h6E/b4</mid><messageInfo mid=\"1_1182_AGdhxEIAAPMVRU03sAyS0Ho0JD4\" toEmail=\"vijaydharap@yahoo.co.in\" subject=\"One time subscription offer\" mimeType=\"multipart/alternative\" externalPopServer=\"\" xapparentlyto=\"vijaydharap@yahoo.co.in\" receivedDate=\"1162626120\" size=\"10201\"><flags isReplied=\"0\" isFlagged=\"0\" isRead=\"1\" isDraft=\"0\" isForwarded=\"0\" isHam=\"0\" isSpam=\"0\" hasAttachment=\"0\" inAddressBook=\"0\"/><from><name>PCQuest</name><email>pcqrse1@cybermedia.co.in</email></from></messageInfo><messageInfo mid=\"1_720_ACRu/NgAAJYpRMcNZQFmVTRFZkA\" toEmail=\"vijaydharap@yahoo.co.in\" subject=\"Great Subscription offer from Living Digital, India's first interactive, digital lifestyle magazine.\" mimeType=\"multipart/alternative\" externalPopServer=\"\" xapparentlyto=\"vijaydharap@yahoo.co.in\" receivedDate=\"1153844008\" size=\"6746\"><flags isReplied=\"0\" isFlagged=\"0\" isRead=\"1\" isDraft=\"0\" isForwarded=\"0\" isHam=\"0\" isSpam=\"0\" hasAttachment=\"0\" inAddressBook=\"0\"/><from><name>Team Living Digital</name><email>shantanup@cybermedia.co.in</email></from></messageInfo><messageInfo mid=\"1_22_ACBu/NgAAPRJRJmdhgLC+h6E/b4\" toEmail=\"vijaydharap@yahoo.co.in\" subject=\"Welcome to Yahoo! Mail Beta\" mimeType=\"text/html\" externalPopServer=\"\" xapparentlyto=\"\" receivedDate=\"1150918022\" size=\"510\"><flags isReplied=\"0\" isFlagged=\"0\" isRead=\"1\" isDraft=\"0\" isForwarded=\"0\" isHam=\"0\" isSpam=\"0\" hasAttachment=\"0\" inAddressBook=\"0\"/><from><name>Yahoo!</name><email>mailbot@yahoo.com</email></from></messageInfo></ListMessagesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";
		ListMessageResponse resp = (ListMessageResponse) parse(str);
	}
	
	public static Object parse(String listMessageResponse) {
		ListMessageResponse resp = null;
		try {
			Digester digester = new Digester();
			digester.setValidating(false);

			digester
			.addObjectCreate(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse",
					ListMessageResponse.class);
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/folder",
					"total", "noOfMessages");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/folder",
					"size","totalSize");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/folder/folderInfo",
					"name", "folderName");
			digester.addObjectCreate(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo",
					MessageInfo.class);
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo",
					"mid", "mid");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo",
					"toEmail", "toEmail");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo",
					"subject", "subject");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo",
					"mimeType", "mimeType");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo",
					"size", "size");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo/flags",
					"isRead", "isRead");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo/from/email",
					"fromEmail");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo/from/name",
					"fromName");
			digester.addSetNext("SOAP-ENV:Envelope/SOAP-ENV:Body/ListMessagesResponse/messageInfo", "addMessage" );
			
			StringReader sr = new StringReader(listMessageResponse);
			resp = (ListMessageResponse) digester.parse(sr);

		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return resp;
	}
}